package com.myph.phctrl_t;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.phctrl_t.R;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

public class SetMCUTimeActivity extends Activity {

    String ip, port;
    StringBuilder response = new StringBuilder();
    TextView txt_date, txt_time;
    Button btn_submit;
    private int mYear, mMonth, mDay;
    private int mHour, mMin, mSec = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_mcu_time);

        ip = Util.getSharedPref(this, "ip");
        port = Util.getSharedPref(this, "port");

        txt_date = (TextView) findViewById(R.id.txt_date);
        txt_time = (TextView) findViewById(R.id.txt_time);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        
        
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
        mHour = calendar.get(Calendar.HOUR_OF_DAY);
        mMin = calendar.get(Calendar.MINUTE);
        updateDate();
        updateTime();
        txt_date.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                DatePickerDialog dpd = new DatePickerDialog(
                        SetMCUTimeActivity.this, Datelistener, mYear, mMonth, mDay);
                dpd.show();// 显示DatePickerDialog组件
            }
        });
        
        txt_time.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                TimePickerDialog tpd = new TimePickerDialog(SetMCUTimeActivity.this, TimeListener, mHour, mMin, true);
                tpd.show();
            }
        });
        
        btn_submit.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });
    }

    
    
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
            	mYear -= 2000;//MCU是从2000开始计数的
            	//month needs to + 1
                String request = "$timcfg," + String.valueOf(mYear) + ","
                        + String.valueOf(mMonth+1) + "," + String.valueOf(mDay)
                        + "," + String.valueOf(mHour) + ","
                        + String.valueOf(mMin) + "," + String.valueOf(mSec)
                        + "#";
                ClientSocketNew csn = new ClientSocketNew(ip, port, handler);
                csn.setNewRequest(request, response);
                csn.start();

                break;
            
            case 1002:
                Toast.makeText(getApplicationContext(), "fail to connect",
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            case 1003:
                if (response != null) {
                    Log.d("", response.toString());
                    String resString = response.toString().trim();
                    if (resString.equals("1")) {
                        Toast.makeText(getApplicationContext(), "设置成功~",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "失败!",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "异常!",
                            Toast.LENGTH_SHORT).show();
                }
                finish();
                break;

            case 1004:
                Toast.makeText(getApplicationContext(), "操作失败",
                        Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                Toast.makeText(getApplicationContext(), "handler error!",
                        Toast.LENGTH_LONG).show();
                break;
            }

        }
    };

    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
       
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear,
                int dayOfMonth) {

            mYear = myyear;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDate();

        }

        
    };
    private void updateDate() {
        txt_date.setText(String.valueOf(mYear) + "-" + (mMonth+1) + "-"
                + mDay);
    }
    private OnTimeSetListener TimeListener = new OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMin = minute;
            updateTime();
        }
        
        

    };
    private  void updateTime() {
        txt_time.setText(String.valueOf(mHour) + ":" + mMin);
    }
}
